package com.lht.cozyCloud.bee.service.impl;

import com.lht.cozyCloud.bee.utils.Constants;
import com.lht.cozyCloud.bee.domain.MessageTemplate;
import com.lht.cozyCloud.bee.mapper.MessageTemplateMapper;
import com.lht.cozyCloud.bee.service.MessageTemplateService;
import com.lht.cozyCloud.common.core.utils.StringUtils;
import com.lht.cozyCloud.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;

/**
 * @description: TODO 
 * @author lht
 * @date 2025/1/6 10:44
 * @version 1.0
 */

@Service
public class MessageTemplateServiceImpl implements MessageTemplateService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private MessageTemplateMapper messageTemplateMapper;

    /**
     * 项目启动时，该bean初始化完成后执行,缓存模板数据
     */
    @PostConstruct  // bean实例化后执行本方法
    public void init()
    {
        loadingTemplateCache();
    }

    private String getCacheKey(String typeKey)
    {
        return Constants.BEE_TEMPLATE_KEY + typeKey;
    }


    // 根据模板类型查询模板
    @Override
    public MessageTemplate selectTemplateByType(String type) {
        MessageTemplate cacheTemplate = redisService.getCacheObject(getCacheKey(type));
        if (!ObjectUtils.isEmpty(cacheTemplate)){
            return cacheTemplate;
        }
        MessageTemplate template = new MessageTemplate();
        template.setType(type);
        MessageTemplate selected = this.messageTemplateMapper.selectTemplate(template);
        if (!ObjectUtils.isEmpty(selected)){
            redisService.setCacheObject(getCacheKey(selected.getType()), selected);
        }
        return selected;
    }

    // 主键查询
    @Override
    public MessageTemplate selectTemplateById(Long templateId) {
        MessageTemplate template = new MessageTemplate();
        template.setTemplateId(templateId);
        return messageTemplateMapper.selectTemplateById(templateId);
    }

    @Override
    public List<MessageTemplate> selectTemplateByIds(Long[] templateIds) {
        return messageTemplateMapper.selectTemplateByIds(templateIds);
    }

    @Override
    public List<MessageTemplate> selectTemplateList(MessageTemplate template) {
        return this.messageTemplateMapper.selectTemplateList(template);
    }

    @Override
    public int insertTemplate(MessageTemplate template) {
        int id = this.messageTemplateMapper.insertTemplate(template);
        if (id > 0) {
            redisService.setCacheObject(getCacheKey(template.getType()), template);
        }
        return id;
    }

    @Override
    public int updateTemplate(MessageTemplate template) {
        int id = this.messageTemplateMapper.updateTemplate(template);
        if (id > 0) {
            // 重置缓存
            redisService.setCacheObject(getCacheKey(template.getType()), template);
        }
        return id;
    }

    @Override
    public void deleteTemplateById(Long templateId) {
        MessageTemplate template = this.selectTemplateById(templateId);
        int i = this.messageTemplateMapper.deleteTemplateById(templateId);
        if (i > 0) {
            redisService.deleteObject(getCacheKey(template.getType()));
        }
    }

    @Override
    public void deleteTemplateByIds(Long[] templateIds) {
        List<MessageTemplate> templateList = this.messageTemplateMapper.selectTemplateByIds(templateIds);
        for (MessageTemplate template : templateList) {
            this.messageTemplateMapper.deleteTemplateByIds(templateIds);
        }
    }

    /**
     * 加载模板类型缓存
     */
    @Override
    public void loadingTemplateCache() {
        List<MessageTemplate> messageTemplates = messageTemplateMapper.selectTemplateList(new MessageTemplate());
        for (MessageTemplate template : messageTemplates)
        {
            redisService.setCacheObject(getCacheKey(template.getType()), template);
        }
    }

    /**
     * 清空缓存
     */
    @Override
    public void clearTemplateCache() {
        Collection<String> templateKeys = redisService.keys(getCacheKey("*"));
        redisService.deleteObject(templateKeys);
    }

    @Override
    public void resetTemplateCache() {
        clearTemplateCache();
        loadingTemplateCache();
    }

    /**
     * 检查类型唯一
     * @param template 参数信息
     * @return
     */
    @Override
    public boolean checkTemplateTypeUnique(MessageTemplate template) {
        if (StringUtils.isEmpty(template.getType())){
            return Constants.UNIQUE;
        }
        String checkType = template.getType();
        MessageTemplate query_template = this.messageTemplateMapper.checkTemplateTypeUnique(checkType);
        if (query_template != null && query_template.getType().equals(checkType)) {
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }
}
